//package com.hotelsystem.config;
//
//import org.springframework.cache.annotation.EnableCaching;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.io.ResourceLoader;
//import org.springframework.data.redis.cache.RedisCacheManager;
//import org.springframework.data.redis.connection.RedisConnectionFactory;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
//import org.springframework.data.redis.serializer.RedisSerializationContext;
//import org.springframework.data.redis.serializer.StringRedisSerializer;
//
//import java.net.UnknownHostException;
//
///**
// * 缓存配置类
// */
//@Configuration
//@EnableCaching //开启注解缓存的支持
//public class CacheConfiguration {
//
//    /**
//     * 替换默认的RedisTemplate
//     * @param redisConnectionFactory
//     * @return
//     * @throws UnknownHostException
//     */
//    @Bean
//    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
//            throws UnknownHostException {
//        RedisTemplate<Object, Object> template = new RedisTemplate<>();
//        template.setConnectionFactory(redisConnectionFactory);
//        //修改默认的序列化器为json序列化器，所有保存的对象会自动转换为json数据
//        template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
//        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//        //修改key的序列化器为string
//        template.setKeySerializer(stringRedisSerializer);
//        template.setHashKeySerializer(stringRedisSerializer);
//        return template;
//    }
//
//    /**
//     * 替换默认的RedisCacheManager
//     * @param redisConnectionFactory
//     * @param resourceLoader
//     * @return
//     */
//    @Bean
//    public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory,ResourceLoader resourceLoader) {
//        //获取RedisCacheConfiguration类
//        org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration.defaultCacheConfig();
//        //修改序列化规则为json
//        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
//        //禁用空值，当查询数据为空时不缓存到redis
//        config = config.disableCachingNullValues();
//        //创建RedisCacheManagerBuilder
//        RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(config);
//        //通过RedisCacheManagerBuilder构建RedisCacheManager
//        return builder.build();
//    }
//}
